﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;

namespace TuKu.PPS
{
    public partial class ppslist : Form
    {
        public ppslist()
        {
            InitializeComponent();
        }

        private void ppslist_Load(object sender, EventArgs e)
        {
            comurlcate.Items.AddRange(Common.Tag.encategory);
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            int catid = comurlcate.SelectedIndex;
            string url = string.Empty;
            StringBuilder sb = new StringBuilder();
            for (int i = int.Parse(txtS.Text); i <= int.Parse(txte.Text); i++)
            {
                if (i == 1)
                {
                    url = "http://tu.pps.tv/" + comurlcate.SelectedItem.ToString() + "/index.html";
                }
                else
                {
                    url = "http://tu.pps.tv/" + comurlcate.SelectedItem.ToString() + "/" + i.ToString() + ".html";
                }
                getString(url,catid,i);
            }
            MessageBox.Show("ok");
        }
        private void getString(string url, int catid, int page)
        {
            WebClient wClient = new WebClient();

           string alt, img, href;
            StringBuilder sb = new StringBuilder();

            string pageData = Encoding.Default.GetString(wClient.DownloadData(url));
            Match urldata = Regex.Match(pageData, @"(?<=<div\s*class=""picList""\s*>)[\s\S]*?(?=</div>)", RegexOptions.IgnoreCase);
            //string partten = @"<li><a href=""http://tu.pps.tv/pics/\d{1,5}/"" title=""(.+)""><img src=""(.+)"" alt=""(.+)"" /></a><span><a href=""(.+)""  title=""(.+)"">(.+)</a></span></li>";
            if (urldata.Success)
            {
                Regex re = new Regex(@"<li>(.+)</li>", RegexOptions.IgnoreCase);//定义链接
                MatchCollection mc = re.Matches(urldata.Value);//获取链接
                for (int i = 0; i < mc.Count; i++)
                {
                    alt = Regex.Match(mc[i].ToString(), @"title=""(.+)""><img", RegexOptions.IgnoreCase).Result(@"$1").ToString();
                    img = Regex.Match(mc[i].ToString(), @"<img src=""(.+)"" alt=""(.+)"" /></a>", RegexOptions.IgnoreCase).Result(@"$1").ToString();
                    href = Regex.Match(mc[i].ToString(), @"<li><a href=""(.+)"" title", RegexOptions.IgnoreCase).Result(@"$1").ToString();
                    sb.Append(" insert into urllist (url,alt,img,orderid,cateid) values ");
                    sb.Append(" ('" + href + "index.html','" + alt.Replace("'","’") + "','" + img + "',"+page.ToString()+"," + catid + " ) ");
                    Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql(sb.ToString());
                    sb.Remove(0, sb.Length);
                }
            }
        }
        /// <summary>
        /// 正则测试代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string text1 = "<body>http://www.google.com/index.htm</body>";
            string text2 = Regex.Replace(text1, @"http://www\.(\S+)\.com/(\S+)\.htm", @"<a href=""http://www.$1.com/$2.htm"">http://www.$1.com/$2.htm<br /></a>", RegexOptions.IgnoreCase);
            //text2.showBox();
            string source = "http://www.sohu.com/newsshow.aspx?id=1231231";
            string strTempContent = System.Text.RegularExpressions.Regex.Replace(source, @"[^\d]*", "");
            //strTempContent.showBox();
            source = textBox1.Text;
            strTempContent = Regex.Match(source, @"<li><a href=""(.+)"" title", RegexOptions.IgnoreCase).Result(@"$1").ToString();
            strTempContent.showBox();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
                      
        }

    }
}
